﻿Imports DataSet_apaTableAdapters
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Partial Class photos_EditarFotot
    Inherits System.Web.UI.Page
    Dim Animales As New animalTableAdapter
    Dim Fotos As New FotosTableAdapter
    Dim IDAnimal As Integer
    Dim sSQL As String
    Dim sdsFotos As New SqlDataSource
    Protected Sub gvAnimales_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvAnimales.Load
        If Not IsPostBack Then
            gvAnimales.DataSource = Animales.GetDataAnimalMod
            gvAnimales.DataBind()
        End If
    End Sub
    Protected Sub txtPadre_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPadre.TextChanged
        gvAnimales.DataSource = Animales.GetAnimalByPadre(txtPadre.Text + "%")
        gvAnimales.DataBind()
        gvAnimales.SelectedIndex = -1
        txtMadre.Text = ""
        txtAnimal.Text = ""
        txtPadre.Focus()
        'iniciaRadioB()
    End Sub
    Protected Sub txtMadre_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMadre.TextChanged
        gvAnimales.DataSource = Animales.GetAnimalByMadre(txtMadre.Text + "%")
        gvAnimales.DataBind()
        gvAnimales.SelectedIndex = -1
        txtAnimal.Text = ""
        txtPadre.Text = ""
        txtMadre.Focus()
        'iniciaRadioB()
    End Sub
    Protected Sub gvAnimales_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvAnimales.PageIndexChanging
        If Len(txtAnimal.Text) = 0 And Len(txtPadre.Text) = 0 And Len(txtMadre.Text) = 0 Then
            Dim newPageNumber0 As Integer = e.NewPageIndex
            gvAnimales.PageIndex = newPageNumber0
            gvAnimales.DataSource = Animales.GetAnimalByClave(txtAnimal.Text + "%")
            gvAnimales.DataBind()
        End If
        If Len(txtAnimal.Text) <> 0 Then
            Dim newPageNumber1 As Integer = e.NewPageIndex
            gvAnimales.PageIndex = newPageNumber1
            gvAnimales.DataSource = Animales.GetAnimalByClave(txtAnimal.Text + "%")
            gvAnimales.DataBind()
        End If
        If Len(txtPadre.Text) <> 0 Then
            Dim newPageNumber2 As Integer = e.NewPageIndex
            gvAnimales.PageIndex = newPageNumber2
            gvAnimales.DataSource = Animales.GetAnimalByPadre(txtPadre.Text + "%")
            gvAnimales.DataBind()
        End If
        If Len(txtMadre.Text) <> 0 Then
            Dim newPageNumber3 As Integer = e.NewPageIndex
            gvAnimales.PageIndex = newPageNumber3
            gvAnimales.DataSource = Animales.GetAnimalByMadre(txtMadre.Text + "%")
            gvAnimales.DataBind()
        End If
        BorraImg()
        gvAnimales.SelectedIndex = -1
    End Sub
    Protected Sub gvAnimales_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvAnimales.SelectedIndexChanged
        '  If Not IsPostBack Then
        'Dim sUrl As String = Request.Url.Authority()
        Dim codigo As String = ""
        Dim nombre As String = gvAnimales.SelectedRow.Cells(4).Text
        'LblNombre.Text = nombre
        IDAnimal = gvAnimales.SelectedIndex
        IDAnimal = gvAnimales.DataKeys(IDAnimal).Item("AnimalId")
        'Dim numero As String = gvAnimales.SelectedRow.Cells(11).Text
        ''Session("NumAnimal") = IDAnimal
        '  End If
        '' lblResultado1.Text = IDAnimal.ToString
        BorraImg()
        iniciaRadioB()
        
        consultaImg(IDAnimal)
    End Sub
    Protected Sub txtAnimal_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAnimal.TextChanged
        Dim Animales As New animalTableAdapter
        gvAnimales.DataSource = Animales.GetAnimalByClave(txtAnimal.Text + "%")
        gvAnimales.DataBind()
        gvAnimales.SelectedIndex = -1
        txtMadre.Text = ""
        txtPadre.Text = ""
        txtAnimal.Focus()
        BorraImg()
        'iniciaRadioB()
    End Sub
    Sub InstTabla(ByRef numero As Integer, ByVal nombre As String)
        Dim fecha As String = Date.Today
        Dim principal As String = "0"
        IDAnimal = gvAnimales.SelectedIndex
        IDAnimal = gvAnimales.DataKeys(IDAnimal).Item("AnimalId")
        Dim iNumber As Integer = IDAnimal
        sSQL = "INSERT INTO Fotos (AnimalId, NoFoto, NombreArchivo, FechaAlta, FotoPrincipal) VALUES  (" + iNumber.ToString + "," + numero.ToString + ", '" + nombre.ToString + "'" + " ,'" + Date.Now.ToShortDateString + "','" + principal.ToString + "'); " & _
        "UPDATE animal set photo = 1 WHERE AnimalId=" & iNumber.ToString & ";"
        Try
            sdsFotos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsFotos.InsertCommand = sSQL
            sdsFotos.Insert()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
    Sub BorraImg()
        Image1.ImageUrl = "~/photos/toro.jpg"
        Image2.ImageUrl = "~/photos/toro.jpg"
        Image3.ImageUrl = "~/photos/toro.jpg"
        Image4.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje1.Text = ""
        LbLMensaje2.Text = ""
        LbLMensaje3.Text = ""
        LbLMensaje4.Text = ""
        LblError1.Text = ""
        LblError2.Text = ""
        LblError3.Text = ""
        LblError4.Text = ""
    End Sub

    Sub consultaImg(ByVal id As Integer)
        Dim Numero As Integer
        Dim Fotos As String
        Dim principal As Boolean
        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim OrdenSQL As SqlCommand = Nothing
        Dim Lector As SqlDataReader = Nothing
        Try
            Dim Consulta As String = "Select AnimalId, NoFoto, NombreArchivo, FechaAlta, FotoPrincipal from Fotos where AnimalId=" & id
            OrdenSQL = New SqlCommand(Consulta, ConexionBD)
            ConexionBD.Open()
            Lector = OrdenSQL.ExecuteReader()
            Dim salida As Integer
            While salida = 0
                If Lector.Read() Then
                    Numero = Lector("NoFoto")
                    Fotos = Lector("NombreArchivo")
                    principal = Lector("FotoPrincipal")
                    If Numero = "1" Then
                        Image1.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje1.Text = Fotos
                        LbLMensaje1.Visible = True
                        RadioButton1.Visible = True
                        RadioButton1.Checked = principal
                    End If
                    If Numero = "2" Then
                        Image2.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje2.Text = Fotos
                        LbLMensaje2.Visible = True
                        RadioButton2.Visible = True
                        RadioButton2.Checked = principal
                    End If
                    If Numero = "3" Then
                        Image3.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje3.Text = Fotos
                        LbLMensaje3.Visible = True
                        RadioButton3.Visible = True
                        RadioButton3.Checked = principal
                    End If
                    If Numero = "4" Then
                        Image4.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje4.Text = Fotos
                        LbLMensaje4.Visible = True
                        RadioButton4.Visible = True
                        RadioButton4.Checked = principal
                    End If
                Else
                    salida = 1
                End If

            End While
        Catch ex As Exception

        End Try
    End Sub
    Sub BorraFotoTablaYFisico(ByVal NombreFoto As String)
        Dim numeroFoto As Integer
        Dim lenFoto As Integer
        Dim codigo As String

        lenFoto = Len(NombreFoto)
        For I = 0 To lenFoto
            If I < lenFoto Then
                codigo = NombreFoto.Substring(I, 1)
                If codigo = "." Then
                    numeroFoto = CInt(NombreFoto.Substring(I - 1, 1))
                    ID = CInt(NombreFoto.Substring(0, I - 2))
                End If
            End If
        Next I
        IDAnimal = gvAnimales.SelectedIndex
        IDAnimal = gvAnimales.DataKeys(IDAnimal).Item("AnimalId")
        Dim iNumber As Integer = IDAnimal

        If numeroFoto = 1 Then
            sSQL = "DELETE FROM Fotos where AnimalId = " & ID & " and " & "NoFoto = " & numeroFoto & ";" & _
            "UPDATE animal set photo = 0 WHERE AnimalId=" & iNumber.ToString & ";"
        Else
            sSQL = "DELETE FROM Fotos where AnimalId = " & ID & " and " & "NoFoto = " & numeroFoto
        End If
        Try
            sdsFotos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsFotos.DeleteCommand = sSQL
            sdsFotos.Delete()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

        Dim archi As String = "NombreFoto"
        Try
            Dim FileToDelete As String
            'Establecer la ruta completa al archivo 
            FileToDelete = Server.MapPath("~/photos/Galeria/" + NombreFoto)
            ' Eliminar un archivo 
            File.Delete(FileToDelete)
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub BtnBorraFoto1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto1.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje1.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image1.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje1.Text = ""
        LblError1.Text = ""
        RadioButton1.Visible = False
    End Sub

    Protected Sub BtnBorraFoto2_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto2.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje2.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image2.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje2.Text = ""
        RadioButton2.Visible = False
    End Sub

    Protected Sub BtnBorraFoto3_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto3.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje3.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image3.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje3.Text = ""
        RadioButton3.Visible = False
    End Sub

    Protected Sub BtnBorraFoto4_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto4.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje4.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image4.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje4.Text = ""
        RadioButton4.Visible = False
    End Sub
    Sub mensajeError(ByVal numero As String, ByVal mensaje As String)
        If numero = 1 Then
            LblError1.Visible = True
            LbLMensaje1.Visible = False
            LblError1.Text = mensaje
        End If
        If numero = 2 Then
            LblError2.Visible = True
            LbLMensaje2.Visible = False
            LblError2.Text = mensaje
        End If
        If numero = 3 Then
            LblError3.Visible = True
            LbLMensaje3.Visible = False
            LblError3.Text = mensaje
        End If
        If numero = 4 Then
            LblError4.Visible = True
            LbLMensaje4.Visible = False
            LblError4.Text = mensaje
        End If
    End Sub
    Sub mensajeClave(ByVal numero As String, ByVal mensaje As String)
        If numero = 1 Then
            LblError1.Visible = False
            LbLMensaje1.Visible = True
            LbLMensaje1.Text = mensaje
        End If
        If numero = 2 Then
            LblError2.Visible = False
            LbLMensaje2.Visible = True
            LbLMensaje2.Text = mensaje
        End If
        If numero = 3 Then
            LblError3.Visible = False
            LbLMensaje3.Visible = True
            LbLMensaje3.Text = mensaje
        End If
        If numero = 4 Then
            LblError4.Visible = False
            LbLMensaje4.Visible = True
            LbLMensaje4.Text = mensaje
        End If
    End Sub
    Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        Dim seleccionado As Integer = 1
        Dim control As RadioButton = RadioButton1
        IDAnimal = gvAnimales.SelectedIndex
        IDAnimal = gvAnimales.DataKeys(IDAnimal).Item("AnimalId")
        'iniciaRadioB()
        RadioSeleccionado(seleccionado, IDAnimal, control)
    End Sub
    Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        Dim seleccionado As Integer = 2
        Dim control As RadioButton = RadioButton2
        IDAnimal = gvAnimales.SelectedIndex
        IDAnimal = gvAnimales.DataKeys(IDAnimal).Item("AnimalId")
        'iniciaRadioB()
        RadioSeleccionado(seleccionado, IDAnimal, control)
    End Sub
    Protected Sub RadioButton3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        Dim seleccionado As Integer = 3
        Dim control As RadioButton = RadioButton3
        IDAnimal = gvAnimales.SelectedIndex
        IDAnimal = gvAnimales.DataKeys(IDAnimal).Item("AnimalId")
        'iniciaRadioB()
        RadioSeleccionado(seleccionado, IDAnimal, control)
    End Sub
    Protected Sub RadioButton4_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
        Dim seleccionado As Integer = 4
        Dim control As RadioButton = RadioButton4
        IDAnimal = gvAnimales.SelectedIndex
        IDAnimal = gvAnimales.DataKeys(IDAnimal).Item("AnimalId")
        'iniciaRadioB()
        RadioSeleccionado(seleccionado, IDAnimal, control)
    End Sub
    Sub iniciaRadioB()
        RadioButton1.Checked = False
        RadioButton1.Visible = False
        RadioButton2.Checked = False
        RadioButton2.Visible = False
        RadioButton3.Checked = False
        RadioButton3.Visible = False
        RadioButton4.Checked = False
        RadioButton4.Visible = False
    End Sub
    Sub RadioSeleccionado(ByVal numero As Integer, ByVal id As Integer, ByRef control As RadioButton)
        Dim numeroFoto As Integer
        Dim actina As String
        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim OrdenSQL As SqlCommand = Nothing
        Dim Lector As SqlDataReader = Nothing
        Try
            Dim Consulta As String = "Select AnimalId, NoFoto, NombreArchivo, FechaAlta, FotoPrincipal from Fotos where AnimalId=" & id
            OrdenSQL = New SqlCommand(Consulta, ConexionBD)
            ConexionBD.Open()
            Lector = OrdenSQL.ExecuteReader()
            Dim salida As Integer
            While salida = 0
                If Lector.Read() Then
                    numeroFoto = Lector("NoFoto")
                    If numero = numeroFoto Then
                        actina = "1"
                        control.Checked = True
                    Else
                        actina = "0"
                        'control.Checked = False
                    End If
                    sSQL = "UPDATE Fotos set FotoPrincipal = " & actina & " WHERE AnimalId=" & id.ToString & " and NoFoto=" & numeroFoto & ";"
                    Try
                        sdsFotos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
                        sdsFotos.UpdateCommand = sSQL
                        sdsFotos.Update()
                    Catch ex As Exception
                        Response.Write(ex.Message)
                    End Try

                Else
                    salida = 1
                End If



            End While
        Catch ex As Exception

        End Try
    End Sub

    
End Class
